<?php

/**
 * This is the model class for table "img_lib".
 *
 * The followings are the available columns in table 'img_lib':
 * @property string $id
 * @property string $url
 * @property string $img_name
 * @property string $title
 * @property integer $status_id
 */
class ImgLib extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return ImgLib the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{img_lib}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status_id', 'numerical', 'integerOnly'=>true),
			array('url', 'length', 'max'=>1000),
			array('img_name', 'file', 'types'=>'jpg', 'on'=>'create'),
			array('img_name', 'file', 'allowEmpty'=>true, 'types'=>'jpg'),
			array('title', 'length', 'max'=>32),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, url, img_name, title, status_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		    'status'=>array(self::BELONGS_TO, 'StatusType', 'status_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'url' => 'URL',
			'img_name' => '图片名称',
			'title' => '标题',
			'status_id' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('img_name',$this->img_name,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('status_id',$this->status_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * do some common field like create/update time and date
	 * @see CActiveRecord::beforeSave()
	 */
  public function beforeSave()
	{
	    if (parent::beforeSave()){
	    
	        if ($this->isNewRecord){
	            $this->create_time = $this->update_time = date('Y-m-d H:i:s');
	            $this->create_user_id = $this->update_user_id = Yii::app()->user->id;
	        }else {
	            $this->update_time = date('Y-m-d H:i:s');
	            $this->update_user_id = Yii::app()->user->id;
	        }
	        return true;
	    }else{
	        return false;
	    }
	}
	
	/**
	 * 
	 * generate random string for a given length.
	 * @param int $len the length
	 * @param string $format use what kind word as seed
	 */
  public function randStr($len=6,$format='ALL_WORD') { 
     switch($format) { 
            case 'ALL_WORD':
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                break;
            case 'ALL' :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
            case 'CHAR' :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
                break;
            case 'NUMBER' :
                $chars = '0123456789';
                break;
            default :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
                break;
        }
        
        mt_srand ( ( double ) microtime () * 1000000 * getmypid () );
        $password = "";
        while ( strlen ( $password ) < $len )
            $password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
        return $password;
  }
  
  public function getLastImg()
  {
      $result = ImgLib::model()->findAll('status_id=1 order by id DESC limit 0,10');
      return $result;
  }
  
  
}